﻿using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Hxsoft;

namespace Hxsoft.AppV1
{
	public class ImageCopyGenerator : IResourceCopyGenerator
	{
		public int? Width { get; set; }
		public int? Height { get; set; }
		public ResizeRectangleType ResizeType { get; set; }

		#region IResourceCopyGenerator 成员
		public byte[] Generate(byte[] source, ref string resourceType)
		{
			System.Drawing.Image image = Image.ResizeImage(new MemoryStream(source), Width, Height, ResizeType);
			MemoryStream stream = new MemoryStream();
			image.Save(stream, GetImageFormat(resourceType));
			return stream.ToArray();
		}
		#endregion

		protected static ImageFormat GetImageFormat(string resourceType)
		{
			switch (resourceType.ToLower())
			{
				case "bmp":
					return ImageFormat.Bmp;
				case "emf":
					return ImageFormat.Emf;
				case "exif":
					return ImageFormat.Exif;
				case "gif":
					return ImageFormat.Gif;
				case "ico":
					return ImageFormat.Icon;
				case "png":
					return ImageFormat.Png;
				case "tiff":
					return ImageFormat.Tiff;
				case "wmf":
					return ImageFormat.Wmf;
				default:
					return ImageFormat.Jpeg;
			}
		}
	}
}